package com.myapplication.ui.listfragment.shoplistactivity.pinglun;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.myapplication.R;
import com.myapplication.bean.CommentBean;
import com.myapplication.config.HUrlConfig;
import com.myapplication.utils.DividerItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by My on 2016/10/15.
 */
public class CommentActivity extends AppCompatActivity implements Contract_Comment.View {
    private List<CommentBean.DataBean.CommentsBean> commentsBeen=new ArrayList<>();
//    private TextView tv_comment_nickname,tv_comment_content,tv_comment_time;
//    private ImageView iv_comment_image;
    private Contract_Comment.Presenter mPresenter;
    private Map<String,String> params=new HashMap<>();
    private CommentRVAdapter adapter;
    private RecyclerView mRecyclerView;
    private Handler handler=new Handler();
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private String type;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        mPresenter=new Presenter_Comment(this);
        params.put(HUrlConfig.key.PLATFORM,HUrlConfig.DefaultValue.PLATFORM);
        params.put(HUrlConfig.key.PAGE,HUrlConfig.DefaultValue.PAGES);
        mRecyclerView= (RecyclerView) findViewById(R.id.id_comment_recyclerview);
        Intent intent=getIntent();
        int list_id=intent.getIntExtra("list_id",0);
//        Log.i("TAG", "--------->onCreate: "+list_id);
        type="goods_list/"+list_id+"/comments.json?";
//        Log.i("TAG", "------------------>onCreate: "+type);
        mPresenter.GetData(type,params);

        //设置刷新时动画的颜色  可以设置4个
        mSwipeRefreshLayout= (SwipeRefreshLayout) findViewById(R.id.swipeRefreshlayout);
        mSwipeRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_bright,
                android.R.color.holo_red_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_green_light
        );
        mRecyclerView.addItemDecoration(new DividerItemDecoration(CommentActivity.this,
                DividerItemDecoration.VERTICAL_LIST));

        
        initRefresh();

    }

    private void initRefresh() {
        //表示当swipeRefreshLayout刷新时触发的监听事件
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.GetData(type,params);
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    public void click(View view) {
        switch (view.getId()){
            case R.id.iv_comment_back:
                finish();
                break;
        }
    }


    @Override
    public void onGetDataSuccess(final List<CommentBean.DataBean.CommentsBean> commentsBeanList) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Log.i("TAG", "-----Comment--PL-------->onGetDataSuccess: "+commentsBeanList.size());
                adapter=new CommentRVAdapter(CommentActivity.this,commentsBeanList);
                mRecyclerView.setAdapter(adapter);
                mRecyclerView.setLayoutManager(new LinearLayoutManager(CommentActivity.this));

            }
        });

    }

    @Override
    public void onGetDataFail(String error) {

    }
}
